﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using jQueryDotNetLib.Controls;

namespace jQueryDotNetLib.Extenders
{

    [ToolboxItem(true)]
    [ToolboxData("<{0}:Animate TriggerSelector=\"\" Selector=\"\" Event=\"\" runat=server />")]
    public class Animate : Control
    {

        [Bindable(true)]
        [Category("Data")]
        [DefaultValue("")]
        [Localizable(true)]
        public string TriggerSelector
        {
            get
            {
                if (ViewState["TriggerSelector"] == null) return "";
                return (string)ViewState["TriggerSelector"];
            }
            set
            {
                ViewState["TriggerSelector"] = value;
            }
        }

        [Bindable(true)]
        [Category("Data")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Selector
        {
            get
            {
                if (ViewState["Selector"] == null) return "";
                return (string)ViewState["Selector"];
            }
            set
            {
                ViewState["Selector"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Event
        {
            get
            {
                if (ViewState["Event"] == null) return "click";
                return (string)ViewState["Event"];
            }

            set
            {
                ViewState["Event"] = value;
            }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string To
        {
            get
            {
                if (ViewState["To"] == null) return "";
                return (string)ViewState["To"];
            }

            set
            {
                ViewState["To"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string From
        {
            get
            {
                if (ViewState["From"] == null) return "";
                return (string)ViewState["From"];
            }

            set
            {
                ViewState["From"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public int ToSpeed
        {
            get
            {
                if (ViewState["ToSpeed"] == null) return 500;
                return (int)ViewState["ToSpeed"];
            }

            set
            {
                ViewState["ToSpeed"] = value;
            }
        }
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public int FromSpeed
        {
            get
            {
                if (ViewState["FromSpeed"] == null) return 500;
                return (int)ViewState["FromSpeed"];
            }

            set
            {
                ViewState["FromSpeed"] = value;
            }
        }

        string resolvedSelector = null;
        public string ResolvedSelector
        {
            get
            {
                if (resolvedSelector == null)
                    resolvedSelector = jQueryDotNetLib.Utility.ResolveSelectors(this.Selector, ((Page.Master != null) ? Page.Master.Controls : Page.Controls));

                return resolvedSelector;
            }
        }
        string resolvedTriggerSelector = null;
        public string ResolvedTriggerSelector
        {
            get
            {
                if (resolvedTriggerSelector == null)
                    resolvedTriggerSelector = jQueryDotNetLib.Utility.ResolveSelectors(this.TriggerSelector, ((Page.Master != null) ? Page.Master.Controls : Page.Controls));

                return resolvedTriggerSelector;
            }
        }
        protected override void Render(HtmlTextWriter writer)
        {
            System.Text.StringBuilder sb = new StringBuilder();

            if (TriggerSelector == "") TriggerSelector = Selector;
            if (Selector == "") Selector = TriggerSelector;

            sb.Append("<script type=\"text/javascript\">");
            sb.Append("jQuery(\"" + ResolvedTriggerSelector + "\").bind('" + Event + "', function(e) {jQuery(\"" + ResolvedSelector + "\").toggle(");

            sb.Append("function(){");
            sb.Append("jQuery(this).animate({");
            sb.Append(From);
            sb.Append("}, " + FromSpeed.ToString() + ");},");
            sb.Append("function(){");
            sb.Append("jQuery(this).animate({");
            sb.Append(To);
            sb.Append("}, " + ToSpeed.ToString() + ");}");
            sb.Append(")});");
            sb.Append("</script>");
            writer.Write(sb.ToString());

        }

    }
}